package info.touret.winecellar.bottle.server.domain;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.annotation.Entity;
import info.touret.winecellar.dao.server.DAOException;
import info.touret.winecellar.dao.server.DataStoreObject;

import javax.persistence.Basic;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * POJO centralisant les infos d une degustation
 * 
 * @author touret-a
 */
@Entity
public class Tasting extends DataStoreObject<Tasting> implements Serializable {

	private static final long serialVersionUID = 7539693502626734680L;

	private static final Logger trace = Logger.getLogger(Tasting.class);

	public Tasting() {
		super();
	}

	/**
	 * @param _id
	 *            id de la degustation
	 * @return La degustation trouvee
	 */
	public static Tasting findTasting(Long _id) {
		Objectify objectify = ObjectifyService.begin();
		return objectify.find(Tasting.class, _id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see info.touret.winecellar.bottle.server.domain.TastingProxy#persist()
	 */

	public void persist() {
		try {
			Objectify objectify = ObjectifyService.begin();
			if (getBottleID() != null) {
				setBottle(new Key<Bottle>(Bottle.class, getBottleID()));
			}
			objectify.put(this);
		} catch (Exception e) {
			trace.error("Erreur durant l insertion:" + e.getMessage());
			throw new DAOException(e);
		}
	}

	private Key<Bottle> bottle;

	public Key<Bottle> getBottle() {
		return bottle;
	}

	public void setBottle(Key<Bottle> bottle) {
		this.bottle = bottle;
	}

	private Long bottleID;

	public Long getBottleID() {
		return bottleID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * info.touret.winecellar.bottle.server.domain.TastingProxy#setBottleID(
	 * java.lang.Long)
	 */
	public void setBottleID(Long bottleID) {
		this.bottleID = bottleID;
	}

	@Temporal(TemporalType.DATE)
	private Date tasted;

	@Basic
	private Integer quantity;

	@Basic
	private Integer note;

	public static List<Tasting> findAllTastings() {
		Objectify objectify = ObjectifyService.begin();
		return objectify.query(Tasting.class).list();
	}

	public Date getTasted() {
		return tasted;
	}

	public void setTasted(Date tasted) {
		this.tasted = tasted;
	}

	public Integer getQuantity() {
		return quantity;
	}

	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	public Integer getNote() {
		return note;
	}

	public void setNote(Integer note) {
		this.note = note;
	}

	/**
	 * Ajoute une degustation et maj le nb des bouteilles encore presentes
	 */
	public void addTasting() {
		Objectify objectify = ObjectifyService.begin();
		Key<Bottle> bottleKey = new Key(Bottle.class, getBottleID());
		Bottle bottle = objectify.get(bottleKey);
		setBottle(bottleKey);
		bottle.setQuantity(bottle.getQuantity() - getQuantity());
		objectify.put(this, bottle);
	}

	/**
	 * @return le nom de la bouteille correspondant a la degustation en cours
	 */
	public String getBottleName() {
		String bottlename = "";
		Objectify objectify = ObjectifyService.begin();
		Key<Bottle> bottleKey = new Key(Bottle.class, getBottleID());
		Bottle bottle = objectify.get(bottleKey);
		trace.debug("Bouteille recuperee= " + bottle);
		bottlename = bottle.getName();

		return bottlename;
	}

}
